﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Plugins.Core.Interfaces;

namespace Plugins.Controls
{
    public class PluginToolStripController<T> : IPluginWindowItem where T : ToolStrip
    {
        private T _toolStrip;

        public string[] GetNames()
        {
            List<string> names = new List<string>();
            if (_toolStrip.Items != null)
            {
                foreach (ToolStripItem item in _toolStrip.Items)
                {
                    names.Add(item.Name);
                }
            }
            return names.ToArray();
        }

        public PluginToolStripController(T statusStrip)
        {
            this._toolStrip = statusStrip;
        }
        private ToolStripItem GetItem(string toolStripName)
        {
            if (_toolStrip.Items.ContainsKey(toolStripName))
            {
                return _toolStrip.Items[toolStripName];
            }
            return null;
        }

        public bool Enable(string toolStripName)
        {
            ToolStripItem item = GetItem(toolStripName);
            if (item != null)
            {
                item.Enabled = true;
                return true;
            }
            return false;
        }
        public bool Disable(string toolStripName)
        {
            ToolStripItem item = GetItem(toolStripName);
            if (item != null)
            {
                item.Enabled = false;
                return true;
            }
            return false;
        }

        public bool AddHandler(string toolStripName, EventHandler handler)
        {
            ToolStripItem item = GetItem(toolStripName);
            if (item != null)
            {
                item.Click += handler;
                return true;
            }
            return false;
        }
        public bool RemoveHandler(string toolStripName, EventHandler handler)
        {
            ToolStripItem item = GetItem(toolStripName);
            if (item != null)
            {
                item.Click -= handler;
                return true;
            }
            return false;
        }

        public bool Add(ToolStripItem item)
        {
            if (item != null)
            {
                if (GetItem(item.Name) == null)
                {
                    _toolStrip.Items.Add(item);
                    return true;
                }
            }
            return false;
        }
        public bool Remove(ToolStripItem item)
        {
            if (item != null)
            {
                if (GetItem(item.Name) != null)
                {
                    _toolStrip.Items.Remove(item);
                    return true;
                }
            }
            return false;
        }
    }
}
